var cart_list = (function () {
    var storage = localStorage.getItem('carts')
    if (storage === null) {
        return []
    }
    return JSON.parse(storage)
})()


var add_callback = $.Callbacks();

add_callback.add(sumGoodsCount)

function sumGoodsCount() {
    var total = cart_list.length

    if ($.cookie('USR') === undefined) {
        $('.ding-safe p span').html(0)
    } else {

        $('.ding-safe p span').html(total)
    }


}
sumGoodsCount()

$(".add_cart").on("click", function () {
    if ($.cookie('USR') === undefined) {
        $('.tishi').show(500)
    }

    var item = localStorage.getItem('detail')
    item = JSON.parse(item)

    var has_same = cart_list.some(function (cart_item) {
        // cart_item 购物车里面的每一个商品;
        if (item.goods_id == cart_item.goods_id) {
            cart_item.count++;
            return true; // 存在相同的商品;
        }
    });

    if (!has_same) {
        // 添加一个新的商品进数组;
        item.count = 1;
        cart_list.push(item);
    }

    // 做个其他的事;
    // 每次操作完数据都保存一下;
    localStorage.setItem("carts", JSON.stringify(cart_list));
    add_callback.fire();
})